package com.banyuan.servlet.types;

import com.banyuan.dao.PageDao;
import com.banyuan.dao.TypesDao;
import com.banyuan.domain.PageInfo;
import com.banyuan.domain.Types;
import com.banyuan.utils.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("/types/listPage")
public class ListPageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String intro = req.getParameter("intro");
        String beginTime = req.getParameter("beginTime");
        String endTime = req.getParameter("endTime");
        // 获取前端的参数
//        ("-----------查询开始----------------");

        // 拼sql语句条件
        StringBuffer condition = new StringBuffer();
        ArrayList<Object> al = new ArrayList<>();
        // 因为数组长度是固定的，所以先定义成集合，这个是？的值

        if (!CommonUtils.isEmpty(name)) {
            condition.append(" and name like ? ");
            al.add("%"+name.trim()+"%");
        }
        if (!CommonUtils.isEmpty(intro)) {
            condition.append(" and intro like ? ");
            al.add("%"+intro.trim()+"%");
        }
        if (!CommonUtils.isEmpty(beginTime)) {
            condition.append(" and createTime >= ? ");
            al.add(beginTime);
        }

        if (!CommonUtils.isEmpty(endTime)) {
            Date date = DateUtils.StringToDate(endTime);
            long time = date.getTime() + (24*60*60*1000)-1;
            date.setTime(time);
            condition.append(" and updateTime <= ? ");
            al.add(date);
        }

        //        ("-----------查询结束----------------");

/*        //        ("-----------分页开始----------------");

        String curr = req.getParameter("curr"); //当前在第几页
        String size = req.getParameter("size"); // 每一页有多少条记录
        Integer currInt = 0;
        Integer sizeInt = 0;

        if(!CommonUtils.isEmpty(curr)) {
            currInt = Integer.parseInt(curr);
        } else {
            currInt = 1;
        }

        if (!CommonUtils.isEmpty(size)) {
            sizeInt = Integer.parseInt(size);
        } else {
            sizeInt = 3;
        }

        // 查出所有符合查询条件的总数据统计值 total

        int total = 0;
        ResultSet resultSet = null;
        int pages = 0;
        try {
            resultSet = TypesDao.selectCountAll(condition.toString(), al.toArray());
            resultSet.next();
            total = resultSet.getInt("count(*)");
            if (total%sizeInt == 0) {
                pages = total/sizeInt;
            } else {
                pages = total/sizeInt + 1;
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeBy(resultSet);
        }

        String limit = "limit "+(currInt-1)*sizeInt+","+sizeInt;
        System.out.println(limit);

        PageInfo pageInfo = new PageInfo(total,currInt,sizeInt,pages);
        req.setAttribute("pageInfo",pageInfo);
        //        ("-----------分页结束----------------");*/

        String limit = PageInfoUtils.pageInfo(req, condition.toString(), al,new TypesDao());



        ResultSet rs = null;
        try {
            rs = TypesDao.selectAll(condition.toString(),limit,al.toArray());
            List<Types> typesList = DBUtils.resultToBeanAll(rs, Types.class);
            req.setAttribute("typesList",typesList);
            req.getRequestDispatcher("/WEB-INF/pages/types/list.jsp").forward(req,resp);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeBy(rs);
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
